<?php
namespace Utuo;

class Http {
	
	/**
	 * POST提交数据
	 *
	 * @param string $url
	 * @param array $data
	 * @param bool 是否返回响应头
	 * @return mixed
	 */
	public static function post($url, $data = '', $resultHeader = 0) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt	($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt	($ch, CURLOPT_SSL_VERIFYHOST, false);
		$return = curl_exec($ch);
		if ( $resultHeader ) {
			$return =  array(
				'content' => $return,
				'header' => self::getHeaderInfo($ch),
			);
		}
		curl_close( $ch );
		return $return;
	}
	
	/**
	 * 获取url内容
	 *
	 * @param string $url
	 * @return string
	 */
	public static function get( $url, $resultHeader = 0 ) {
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, false);
		curl_setopt	($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt	($ch, CURLOPT_SSL_VERIFYHOST, false);
		$return = curl_exec($ch);
		if ( $resultHeader ) {
			$return =  array(
				'content' => $return,
				'header' => self::getHeaderInfo($ch),
			);
		}
		curl_close($ch);
		return $return;
	}
	
	
	protected static function getHeaderInfo( $ch ) {
		$header = curl_getinfo($ch);
		return $header;
	}
	
}